<?php
namespace app\common\logic;
use app\common\model\DataSettingModel;
class DataSettingLogic{
    private  $settings;
    private  $keys = [
        'site',
        'open',
        'sms',
        'payment',
        'oss'
    ];
    public function __construct() {
        $settings = cache('data-setting');
        if(empty($settings)){
            $settings = $this->initData();
            cache('data-setting',$settings,600);//给个10分钟缓存
        }
        $this->settings = $settings;
    }
    
    //初始化Data
    public function initData(){
        $DataSettingModel = new DataSettingModel();
        $settings = $DataSettingModel->fetchAll();
        if(empty($settings)){
            $settings = $this->getDefault();
            foreach($settings as $k=>$v){
                $DataSettingModel = new DataSettingModel();
                $DataSettingModel->save([
                    'k' => $k,
                    'v' => $v,
                ]);
            }
        }
        return $settings;
    }
    
    
    //datas 方便后台以后新增参数
    public function saveData($key,$datas){ 
        if(!in_array($key, $this->keys)) return false;
        $DataSettingModel = new DataSettingModel();
        $DataSettingModel->update([
            'v' => $datas
        ],['k'=>$key]);
        $this->refreshCache();
        return true;
    }
    
    public function getData($key = 'site'){
        if(!in_array($key, $this->keys)) return [];
        return $this->settings[$key] ?? [];
    }
    
    
    private function refreshCache(){ //重新刷新一下KEY
        $DataSettingModel = new DataSettingModel();
        $settings = $DataSettingModel->fetchAll();
        cache('data-setting',$settings,600);//给个10分钟缓存
    }
    
    //默认的SITE 配置
    private function getDefault(){
        return [
           'site' => [
                'name'                         => '伯言科技',
                'logo_round'                   => '',//圆形LOGO
                'logo_white_account_login'     => '',//白色LOGO尺寸1
                'logo_white_account_manage'    => '',//白色LOGO尺寸2
                'logo_white_admin_login'       => '',//总后台登录LOGO
                'logo_white_admin_manage'      => '',//总后台白色管理LOGO
                'tel'                          => '',//电话号码 
                'domain'                       => 'ys.boyanhy.com',//网站域名
            ],
            'open' => [
                'app_id'         => '',
                'app_secret'     => '',
                'app_token'      => '',
                'app_key'        => '',
                'miniapp_id'     => '',
                'miniapp_auth'   => '',
            ],
            'oss'  => [
                'type'      => 0,//0 本地存储 1七牛云 2阿里  3腾讯,
                'domain'    => '',//域名 https://ysimg.boyanhy.com 不包含后面的/
                'qiniu'     => [
                                    'ak'                => '',
                                    'sk'                => '',
                                    'bucket'            => '',
                                ],
                'ali'       => [
                                    'access_key_id'     => '',
                                    'access_key_secret' => '',
                                    'bucket'            => '',
                                    'endpoint'          => '',
                                ],
                'qcloud'    => [    
                                    'app_id'            => '',
                                    'secret_id'         => '',
                                    'secret_key'        => '',
                                    'bucket'            => '',
                                    'region'            => '',
                                ],  
            ],
            'sms'   => [
                'type'      => 1,//1七牛云 2阿里云 3腾讯云
                'captcha_id' => '',
                'qiniu'     => [
                                'ak'         => '',
                                'sk'         => '',
                                ],
                'ali'       => [
                                'access_key_id'             => '',
                                'access_key_secret'         => '',
                                'sign'                      => '',
                                ],
                'qcloud'    => [
                                'app_id'            => '',
                                'secret_id'         => '',
                                'secret_key'        => '',
                                'sign'              => '', //短信签名    
                                ],
                'give_num'  => 10, //赠送条数
                'price'     => [
                    ['num'=>100,'price'=>10],
                    ['num'=>500,'price'=>50],
                    ['num'=>1000,'price'=>80],
                    ['num'=>2000,'price'=>150],
                    ['num'=>5000,'price'=>258],
                    ['num'=>10000,'price'=>500],
                ], //价格设置
            ],
            'payment' => [
                    //服务商支付部分
                   'ser_mach_id'   => '',
                   'ser_app_id'    => '',
                   'ser_api_auth'  => '',
                   'ser_api_cert'  => '',
                   'ser_api_key'   => '',
                   //平台购买短信续费等部分
                   'mach_id'   => '',
                   'app_id'    => '',
                   'api_auth'  => '',
                   'api_cert'  => '',
                   'api_key'   => '',
            ],
        ];
    }
    
 
    
}